package jw.webapi.homePage;


import jw.model.chart.WxChart;
import jw.model.product.WxProduct;
import jw.model.product.WxProductImage;
import jw.repository.chart.WxChartRepository;
import jw.repository.product.WxProductImageRepository;
import jw.repository.product.WxProductRepository;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 轮播图接口
 * Created by 天赐美颜郭德纲 on 2020/01/06.
 */
@Controller
@CrossOrigin
@RequestMapping("wx/homePage/")
public class HomePageWebApi {

    @Autowired
    private WxChartRepository wxChartRepository;

    @Autowired
    private WxProductRepository wxProductRepository;

    @Autowired
    private WxProductImageRepository wxProductImageRepository;

    /****
     * 首页接口
     * @return
     */
    @RequestMapping(value = "homeList",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> homeList(){
        Map<String, Object> result = new HashMap<>();
        //轮播图
        List<WxChart> chartList = wxChartRepository.findByIsDelete(0);
        List<Map<String,Object>> chartData = new ArrayList<>();
        for (WxChart chart : chartList) {
            Map<String,Object> chartObj = new HashedMap();
            chartObj.put("id",chart.getId());
            chartObj.put("url",chart.getPicture());
            chartObj.put("clickUrl",chart.getUrl());
            chartData.add(chartObj);
        }
        result.put("banner",chartData);

        //首页新品产品列表
        List<WxProduct>productList = wxProductRepository.findByIsNew(1);
        List homeProductList = new ArrayList();
        for (WxProduct product : productList){
            Map<String,Object> productObj = new HashedMap();
            productObj.put("id",product.getId());
            productObj.put("name",product.getName());
            productObj.put("retailPrice",product.getRetailSale());
            List<WxProductImage> productImages = wxProductImageRepository.findByProductIdAndIsHomePage(product.getId(),1);
            productObj.put("picUrl",productImages.get(0).getUrl());
            homeProductList.add(productObj);
        }
        result.put("newGoodsList",homeProductList);

        result.put("errMsg", "成功");
        result.put("returnValue", 1);
        return result;
    }


}
